<template>
  <el-dialog
    :visible="showVisible"
    width="800px"
    center
    :show-close="false"
    :append-to-body="true"
  >
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>参数配置</span>
        <span class="el-icon-close g9 f26 poi" @click="handleCancel"></span>
      </div>
    </div>
    <div class="pl30 pr30">
      <el-button v-if="!!processCanEdit" class="header-btn" type="text" @click="handleParamsAdd"
      >添加参数
      </el-button>
      <el-form :model="formData" ref="form" label-width="120px">
        <el-table
          class="mt10"
          ref="table"
          :data="formData.params"
          style="width: 100%"
          row-key="id"
          stripe
          border
        >
          <el-table-column prop="name" label="参数名" width="300">
            <template slot-scope="scope">
              <el-form-item
                v-if="processCanEdit"
                label-width="0px"
                :prop="'params.' + scope.$index + '.name'"
                :rules="[
                {
                  required: true,
                  message: '参数名不能为空',
                  trigger: 'change',
                },
                { validator: nameValidator, trigger: 'change' },
              ]"
              >
                <el-input
                  v-model="scope.row.name"
                  placeholder="请输入参数名"
                  :maxlength="64"
                  show-word-limit
                ></el-input>
              </el-form-item>
              <span v-else>
                {{scope.row.name}}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="value" label="值">
            <template slot-scope="scope">
              <el-form-item
                v-if="processCanEdit"
                label-width="0px"
                :prop="'params.' + scope.$index + '.value'"
                :rules="[
                {
                  required: true,
                  message: '参数值不能为空',
                  trigger: 'change',
                }
              ]"
              >
                <el-input
                  v-model="scope.row.value"
                  placeholder="请输入参数值"
                  :maxlength="64"
                  show-word-limit
                >
                </el-input>
              </el-form-item>
              <span v-else>
                {{scope.row.value}}
              </span>
            </template>
          </el-table-column>
          <el-table-column v-if="processCanEdit" prop="actions" label="操作" width="80">
            <template slot-scope="scope">
              <el-button
                class="link-danger"
                type="text"
                @click="handleDelete(scope.row, scope.$index)"
              >移除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>
    <div  slot="footer" class="tr">
      <el-button v-if="processCanEdit" @click="handleCancel">{{ $t('sys.cancel') }}</el-button>
      <el-button v-if="processCanEdit" type="primary" @click="submit">{{ $t('sys.confirm') }}</el-button>
      <el-button type="primary" v-if="!processCanEdit"  @click="handleCancel">{{ $t('sys.close') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "ParamConfigModal",
  props: {
    processCanEdit: {
      type: Boolean,
      default: ()=>true,
    },
  },
  data() {
    return {
      showVisible: false,
      formData: {
        params: [],
        paramType: 'todoParams'
      },
    };
  },

  computed: {
  },

  created() {
  },

  methods: {
    showModal(dataStr, paramType='todoParams') {
      const obj = JSON.parse(dataStr)
      this.paramType = paramType
      this.showVisible = true;
      var arr = []
      console.log(1111)
      for (let i in obj) {
        let o = {};
        o['name'] = i;
        o['value'] = obj[i];
        arr.push(o)
      }
      this.formData.params = arr
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleParamsAdd() {
      const id = String(Date.now());
      const item = {
        id,
        name: "",
        value: "",
      };
      this.formData.params.push(item);
    },
    nameValidator(rule, value, callback) {
      const names = this.formData.params.map((item) => item.name);
      if (names.filter((name) => name === value).length > 1) {
        callback(new Error("参数已存在"));
      } else {
        callback();
      }
    },
    handleDelete(record, targetIndex) {
      this.formData.params = this.formData.params.filter(
        (item, index) => index !== targetIndex
      );
    },
    async submit() {
      try {
        await this.$refs.form.validate();
        if(!this.formData.params?.length){
          this.$emit('ok',  '', this.paramType)
        } else {
          let obj = {};
          (this.formData.params || []).map((param) => {
            obj[param.name] = param.value
          })
          this.$emit('ok',  JSON.stringify(obj), this.paramType)
        }
        this.handleCancel()
      } catch (error) {
        console.log(error);
      }
    },
    handleCancel() {
      this.showVisible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.el-table ::v-deep .el-form-item {
  margin-bottom: 0;
}
</style>
